<?php
require_once 'pages/TMSItemPage.inc';

/**
 * @author Admin
 * @property TCatalogProductModel $ProductModel
 * @property TMSProductSitePublisher $ProductPublisher
 * @property TYandexDirectPublisher $YandexPublisher
 * @property TMSProductImageModel $ProductImageModel
 */

class TIntegralProductPage extends TMSItemPage {
/**
 * @var TCatalogProductModel
 */	
	protected $_ioc_product_model_;
	
/**
 * @var TMSProductSitePublisher
 */	
	protected $_ioc_product_publisher_;

/**
 * @var TYandexDirectPublisher
 */		
	protected $_ioc_yandex_publisher_;
/**
 * @var TMSProductImageModel
 */	
	protected $_ioc_product_image_model_;
/**
 * @var TCMSCategoryModel
 */	
	protected $_ioc_categories_model_;
	
/**
 * @var TCMSPageCategoryModel
 */	
	protected $_ioc_page_categories_model_;
	
	protected $productItem;
	
	protected $pageItem;
	
	protected $yandexItem;
	
	private $_list_page_;
	
	private $_categories_;
	
	private $_page_categories_;
	
	protected $_conf_frontend_root_ = "/";
	
	public function Handle(THttpRequest $request){
		$this->request = $request;
		$this->productItem = $this->getItem();
		if ($this->productItem->ItemId()){
			$this->pageItem = $this->ProductPublisher->ProductIssue($this->productItem->Base);
			$this->yandexItem = $this->YandexPublisher->ProductIssue($this->productItem->Base);
		}
		if (is_null($this->pageItem))
			$this->pageItem = $this->ProductPublisher->GetDummyItem();
		
		parent::Handle($request);
	}	
	
	protected function getItem(){
		if ($this->request->id)
			return $this->ProductModel->GetItem($this->request->id);
		return $this->ProductModel->GetDummyItem();
	}
	
	public function __set($nm,$value){
		switch ($nm){
			case 'SaveActionTitle':$this->_save_title_ = $value;break;
			case 'BackActionTitle':$this->_back_title_ = $value;break;
			case 'Print':$this->_print_ = $value;break;
			default:parent::__set($nm,$value);break;
		}
	}
	
	public function __get($nm){
		switch ($nm){
			case 'SaveActionTitle':return $this->_save_title_;break;
			case 'BackActionTitle':return $this->_back_title_;break;
			case 'ProductProperties':{
				$props = $this->productItem->Properties();
				if (empty($this->hideProperties))
					return $props;
				$result = array();
				foreach ($props as $p)
					if (!in_array($p->Name(), $this->hideProperties))
						$result[] = $p;
				return $result;	
			}break;
			case 'PageProperties':{
				$props = $this->pageItem->Properties();
				if (empty($this->hideProperties))
					return $props;
				$result = array();
				foreach ($props as $p)
					if (!in_array($p->Name(), $this->hideProperties))
						$result[] = $p;
				return $result;	
			}break;
			case 'Product':return $this->productItem;break;
			case 'ProductPage':return $this->pageItem;break;
			case 'YandexItem':return $this->yandexItem;break;
			case 'Categories':{
				if (is_null($this->_categories_))
					$this->_categories_ = $this->CategoriesModel->GetContents(null,false);
				return $this->_categories_;
			}break;
			case 'PageCategories':{
				if (is_null($this->_page_categories_)){
					$this->_page_categories_ = array();
					if ($this->pageItem->ItemId())
						foreach ($this->pageItem->Categories as $category)
							$this->_page_categories_[] = $category->Id;
				}
				return $this->_page_categories_;
			}break;
			case 'Path':{
				if (is_null($this->_list_page_))
					$this->_list_page_ = $this->service->ListPage($this->ProductModel->ClassName());
				return array($this->_list_page_->Url()=>$this->_list_page_->Title());
			}break;
			case 'ListUrl':return $this->service->ListUrl($this->ProductModel->ClassName());break;
			case 'PrintUrl':return $this->_print_?$this->service->Url($this->_print_,null,null,array('id'=>$this->productItem->ItemId())):null;break;
			case 'FrontProductUrlQuery':return ($this->pageItem->ItemId())?($this->FrontendRoot.'?service=json&method=ItemUrl&class=TMSProductPage&id='.$this->pageItem->ItemId().'&slug='.$this->pageItem->Slug):null;break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function Title(){
		return ($this->productItem->ItemId())?$this->productItem->Name:'Новый товар';
	}
	
	protected function doProductSave($data){
		if ($this->productItem->ItemId())
			$this->productItem = $this->ProductModel->EditItem($this->productItem->ItemId(), $data);
		else 
			$this->productItem = $this->ProductModel->CreateItem($data);
		
		return $this->productItem;	
	}
	
	protected function doPageSave($data,$categories){
		if (!$data['Title'])
			$data['Title'] = $this->productItem->Name;
		if (!$this->pageItem->ItemId()){
			if (!isset($data['IsPublished']))
				$data['IsPublished'] = false;
			$this->pageItem = $this->ProductPublisher->PublishProduct($this->productItem->Base,array_keys($categories));
			$this->pageItem = $this->ProductPublisher->EditItem($this->pageItem->ItemId(), $data);
		} else { 
			$this->pageItem = $this->ProductPublisher->EditItem($this->pageItem->ItemId(), $data);
			$this->PageCategoriesModel->MovePage($this->pageItem->Base, array_keys($categories));
		}
		return $this->pageItem;
	}

	protected function doImagesAdd($data){
		foreach ($data as $file)
			$this->ProductImageModel->CreateItem(array('Image'=>$file,'Product'=>$this->productItem));	
	}
	
	protected function doImagesDelete($data){
		foreach ($data as $id=>$v)
			if ($v)
				$this->ProductImageModel->DeleteItem($id);
	}
	
	protected function doImagesUpdate($imagesdesc = array(),$imagesorder = array()){
		foreach ($imagesdesc as $id=>$desc)
			$this->ProductImageModel->EditItem($id, array('Description'=>$desc,'OrderNumber'=>isset($imagesorder[$id])?$imagesorder[$id]:null));
	}
	
	public function http_Save($product = array(),$productpage = array(), $categories = array()){
		/*$need_redirect = false;*/
		/*if (!$this->productItem->ItemId())
			$need_redirect = true;*/
		$this->doProductSave($product);
		$this->doPageSave($productpage,$categories);
		/*if ($need_redirect)
			$this->Application()->Redirect($this->Url());*/
	}
	
	public function http_EditImages($addimages = array(),$deleteimages = array(),$imagesdesc = array(),$imagesorder = array()){
		$this->doImagesAdd($addimages);
		$this->doImagesUpdate($imagesdesc,$imagesorder);
		$this->doImagesDelete($deleteimages);
		$this->ReloadItem();
	}
	
	public function NeedsToken(){
		return $this->YandexPublisher->NeedsToken();
	}
	
	public function TokenRequestUrl($response_type = 'token',$popup = false){
		return $this->YandexPublisher->TokenRequestUrl('token',$popup);
	}
		
	public function http_PublishToYandex(){
		$this->YandexPublisher->PublishProduct($this->productItem->Base, array());
	}
	
	public function http_YandexDirectApply($Campaign,$Title,$Text,$MinusKeywords,$PhraseList = array(),$Stage){
		$this->YandexPublisher->EditItem($this->YandexItem->ItemId(), array(
				'Campaign'=>$Campaign,
				'Title'=>$Title,
				'Text'=>$Text,
				'MinusKeywords'=>$MinusKeywords,
				'PhraseList'=>$PhraseList,
				'Stage'=>$Stage
		));
	}

	public function ReloadItem(){
		if ($this->productItem){
			$this->productItem = $this->ProductModel->GetItem($this->productItem->ItemId());
			$this->pageItem = $this->ProductPublisher->ProductIssue($this->productItem->Base);
		}
	}
	
	protected function processUrlParameters(array $parameters){
			$parameters['id'] = $this->productItem->ItemId();
		return $parameters;
	}
	
	protected function listPath(){
		return substr($this->Name(),0,strrpos($this->Name(), '/'));
	}
}